﻿//+-------------------------------------------------------------------------------------------------------------------------------------------------------
//
//Description: HardwareProfileViewModel
//Author: Aravind Ramarathinam (aravr@microsoft.com)
//Copyright © Microsoft Corporation
//Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
//You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
//
//THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
//INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. 
//See the Apache Version 2.0 License for specific language governing permissions and limitations under the License.
// 
//--------------------------------------------------------------------------------------------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using VmroleAuthor.Models;

namespace VmroleAuthor.ViewModels
{
    public class HardwareProfileViewModel : TreeViewItemViewModel, IDataErrorInfo, IProduceValidationResults
    {
        private readonly VmRoleResourceDefinitionViewModel vmRoleResourceDefinitionViewModel;
        private readonly VMHardwareProfile hardwareProfile;

        private EnumParameterizableProperty vmSize;

        public static class PropertyNames
        {
            public const string Summary = "Summary";
            public const string VMSize = "VMSize";
        }

        public HardwareProfileViewModel(
            VMHardwareProfile hardwareProfile,
            VmRoleResourceDefinitionViewModel resourceDefinitionViewModel)
            : base(resourceDefinitionViewModel, true)
        {
            if (hardwareProfile == null)
            {
                hardwareProfile = new VMHardwareProfile();
            }
            this.hardwareProfile = hardwareProfile;
            this.vmRoleResourceDefinitionViewModel = resourceDefinitionViewModel;
            this.ValidationResults = new ObservableCollection<CloudServiceModelValidationResult>();     
    
            if (this.vmRoleResourceDefinitionViewModel.IsNewResdefCreatedByTheTool)
            {
                //Auto-parameterize VMSize 
                this.VMSize.PropertyValue = ParametersViewModel.AutoGenerateMoniker;
            }
        }

        public string Summary
        {
            get
            {                
                return "Hardware Profile";
            }
        } 

        /// <summary>
        /// RoleSize
        /// </summary>
        public EnumParameterizableProperty VMSize
        {
            get
            {
                if (this.vmSize == null)
                {
                    Func<string> paramValueGetter = () => this.hardwareProfile.VMSize;
                    Action<string> paramValueSetter = delegate(string value) { this.hardwareProfile.VMSize = value; };
                    Func<string> autoParamNameGetter = () => ParametersViewModel.VMRolePrefix + PropertyNames.VMSize;
                    ViewDefParamInfo viewDefParamInfo = new ViewDefParamInfo
                    {
                        IsRecommendedToBeParameterized = true,
                        ViewDefParameterType = ViewDefParameterType.VMSize,                        
                        DefaultEnglishLabelText = "Computer Size",                       
                        Options = new List<string>(Enum.GetNames(typeof(RoleSize)))
                    };

                    this.vmSize = new EnumParameterizableProperty(
                        PropertyNames.VMSize,
                        paramValueGetter,
                        paramValueSetter,
                        autoParamNameGetter,
                        ParameterType.String,
                        "Computer size",
                        this.vmRoleResourceDefinitionViewModel.ResourceDefinitionParametersViewModel,
                        typeof(RoleSize),
                        true,
                        viewDefParamInfo);
                }
                return this.vmSize;
            }
        }

        public string this[string propertyName]
        {
            get
            {
                return null;
            }
        }

        public string Error { get; set; }

        public ObservableCollection<CloudServiceModelValidationResult> ValidationResults { get; private set; }

        public void UpdateValidationResults()
        {
            this.IsExpanded = true;
            ModelValidationResultsHelper.UpdateValidationResults(this, typeof(PropertyNames));
            //Add any other cross-field validations error            
        }       
    }
}
